package com.bigShen.pattern.observer.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author : lei.shen
 * @date : 2018/10/7
 */
public class MouseServiceCGLIB implements MethodInterceptor {

    private Object element;

    public MouseServiceCGLIB(Object element) {
        this.element = element;
    }

    public Object getInstance() {

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(MouseServiceB.class);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

        System.out.println("Before CGLIB!");

        methodProxy.invokeSuper(o, args);
        //method.invoke(element, args);

        System.out.println("After CGlib");

        return null;
    }

}
